<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/11/20
 * Time: 18:15
 */

namespace app\modules\dataMapper;


use yii\base\InvalidArgumentException;

class UserMapper
{
    private $_adapter;

    /**
     * UserMapper constructor.
     * @param StorageAdapter $storageAdapter
     */
    public function __construct(StorageAdapter $storageAdapter)
    {
        $this->_adapter = $storageAdapter;
    }

    public function findById(int $id): User
    {
        $result = $this->_adapter->find($id);

        if ($result) {
            return $this->mapRowToUser($result);
        } else {
            throw new InvalidArgumentException('User is not found');
        }
    }

    /**
     * 转换User对象
     *
     * @date   2018/11/22
     * @author edmmss
     * @param array $state
     * @return User
     */
    private function mapRowToUser(array $state): User
    {
        return User::fromState($state);
    }
}